function addTwo(x) {
  return x + 2;
}

function addThree(x) {
  return x + 3;
}

function addFive(x) {
  return x + 5;
}

// reduce 方法的第2个参数是初始值，之后进行数组的迭代运算
function addTen(x) {
  return [addTwo, addThree, addFive].reduce(
    (promise, fn) => promise.then(fn),
    Promise.resolve(x)
  );
}

function compose(...fns) {
  return (x) =>
    fns.reduce((promise, fn) => promise.then(fn), Promise.resolve(x));
}

let addTen = compose(addTwo, addThree, addFive);

addTen(8).then(console.log);
